package org.example.controller;

import org.example.entity.Teachers;
import org.example.service.TeaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/teas")
public class TeaController {
    @Autowired
    private TeaService teaService;

    @GetMapping
    public String getList(Teachers teachers, Model model){
        model.addAttribute("teaList",teaService.getList(teachers));
        return "tea/list";
    }

    @GetMapping("/del/{tid}")
    public String del(@PathVariable Long tid){
        teaService.delete(tid);
        return "redirect:/teas";
    }

    @GetMapping("/update/{tid}")
    public String update(@PathVariable Long tid,Model model){
        model.addAttribute("teacher",teaService.get(tid));
        return "tea/update";
    }

    @PostMapping("/update")
    public String update(Teachers teachers){
        int count = teaService.update(teachers);
        if (count == 1){
            return "redirect:/teas";
        }
        return "tea/update";
    }

    @GetMapping("/add")
    public String add(){
        return "tea/add";
    }

    @PostMapping("/add")
    public String add(Teachers teachers){
        int count = teaService.add(teachers);
        if (count == 1){
            return "redirect:/teas";
        }
        return "tea/add";
    }

}
